#include "HttpServer.hpp"

#include "Log.hpp"
using namespace MyLogModule;

void Login(HttpRequest& req,HttpResponse& resp)
{
    LOG(LogGrade::NORMAL) << "进入Login方法";
    cout <<"path:" <<req.getPath() << endl;
    cout <<"args:" <<req.getArgs() << endl;
    //1. 解析请求，获得参数
    //  /login? username=zhang&password=123456
    string args = req.getArgs();

    //2. 根据参数，访问数据库，验证合法性
    //....

    //3. 构建应答
    //设置状态码、状态描述、Content-length、Content-type、body
    resp.SetStatusCode(200);
    resp.SetStatusDesc(resp.GetStatusCode());
    string body = req.GetContent("wwwroot/loginSuccess.html");
    resp.SetContent(body);
    int length = body.size();
    resp.SetHeadKV("Content-length",to_string(length));
    resp.SetHeadKV("Content-type",".html");
    
    // resp.SetStatusCode(302);
    // resp.SetHeadKV("Location"," http://www.baidu.com");
}

int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        cout << "Usage: HttpServer port" << endl;
        return 1;
    }
    unique_ptr<HttpServer> httpPtr = make_unique<HttpServer>(stoi(argv[1]));
    httpPtr->Register("/login",Login);
    httpPtr->Start();
    return 0;
}